package com.lanju.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.ServiceCommentDao;
import com.lanju.dao.ServiceDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.ServiceComment;
import com.lanju.entity.Service_User_Rel;
import com.lanju.entity.User;
import com.lanju.service.ServiceCommentService;
@Service
public class ServiceCommentServiceImpl implements ServiceCommentService{

	@Autowired
	ServiceCommentDao serviceCommentDao;
	@Autowired
	ServiceDao serviceDao;
	@Autowired
	UserDao userDao;
	
	@Override
	  @Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> insertServiceComment(Map<String, Object> map) {
		HashMap<String, Object> result=new HashMap<>();
		try {
			Service_User_Rel order=serviceDao.getServiceDetail(map.get("orderId").toString());
			User u=userDao.getById(map.get("userId").toString());
			ServiceComment serviceComment=new ServiceComment();
			serviceComment.setCommentId(UUID.randomUUID().toString());
			serviceComment.setCommentContent(map.get("content").toString());
			serviceComment.setCreateTime(new Date());
			serviceComment.setCreator(map.get("userId").toString());
			serviceComment.setStatus(0);
			serviceComment.setHeadImg(u.getHeadImg()==null?null:u.getHeadImg());
			serviceComment.setUserName(u.getNickName());
			serviceComment.setOrderId(order.getOrderId());
			serviceComment.setLevel(Integer.valueOf(map.get("level").toString()));
			if(order !=null){
				serviceComment.setServiceId(order.getServiceId());
			}
			serviceCommentDao.insertServiceComment(serviceComment);
			result.put("success", true);
			result.put("serviceId", order.getServiceId());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getCommentList(Map<String, Object> map) {
		HashMap<String, Object> result=new HashMap<>();
		List<ServiceComment> resultList=new ArrayList<>();
		try {
			java.util.List<ServiceComment> list= serviceCommentDao.getCommentList(map);
			if(list !=null && list.size()>0){
				for(int i=0;i<list.size();i++){
					ServiceComment sc=list.get(i);
					User u=userDao.getById(sc.getCreator());
					sc.setUserName(u.getNickName());
					sc.setHeadImg(u.getHeadImg());
					resultList.add(sc);
				}
			}
			result.put("rows", resultList);
			result.put("total", serviceCommentDao.getCommentListCount(map));
			result.put("offset", Integer.valueOf(map.get("offset").toString()));
			result.put("success", true);
		} catch (Exception e) {
			result.put("rows", null);
			result.put("total", 0);
			result.put("success", false);
			e.printStackTrace();
		}
		return result;
	}

}
